package com.smart.controller;

import com.smart.common.PageInfo;
import com.smart.common.WebResponse;
import com.smart.common.annotation.WebAccess;
import com.smart.common.exception.BusinessException;
import com.smart.model.request.OperateRoleRequest;
import com.smart.model.request.RoleAddRequest;
import com.smart.model.request.RoleUpdateRequest;
import com.smart.model.vo.OperatePermissionVO;
import com.smart.model.vo.OperateRoleVO;
import com.smart.model.vo.OperateTreePermissionVO;
import com.smart.service.OperatePermissionService;
import com.smart.service.OperateRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author zhanghong
 */
@RestController
@RequestMapping("/role")
@Api(tags = {"运营平台#运营角色模块"})
public class OperateRoleController {

    @Autowired
    private OperateRoleService operateRoleService;
    @Autowired
    private OperatePermissionService operatePermissionService;

    @WebAccess(login = true)
    @ApiOperation(value = "根据主键查询", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/get/{id}")
    public WebResponse<OperateRoleVO> getByKey(HttpServletRequest request, @PathVariable("id") long id) {
        return WebResponse.build(operateRoleService.getByKey(id));
    }

    @WebAccess(login = true)
    @ApiOperation("分页查询")
    @PostMapping("/pageQuery")
    public WebResponse<PageInfo<OperateRoleVO>> pageQuery(@RequestBody OperateRoleRequest request) {
        return WebResponse.build(operateRoleService.pageQuery(request));
    }

    @WebAccess(login = true)
    @ApiOperation("列表查询")
    @PostMapping("/listQuery")
    public WebResponse<List<OperateRoleVO>> listQuery(@RequestBody OperateRoleRequest request) {
        return WebResponse.build(operateRoleService.listQuery(request));
    }

    @WebAccess(login = true)
    @ApiOperation("新增")
    @PostMapping("/add")
    public WebResponse<Boolean> add(@Valid @RequestBody RoleAddRequest request) {
        return WebResponse.build(operateRoleService.add(request));
    }

    @WebAccess(login = true)
    @ApiOperation("修改")
    @PostMapping("/update")
    public WebResponse<Boolean> updateByKey(@Valid @RequestBody RoleUpdateRequest request) {
        return WebResponse.build(operateRoleService.updateByKey(request));
    }

    //@WebAccess(login = true)
    //@ApiOperation(value = "删除角色", httpMethod = "GET")
    //@ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    //@ApiImplicitParams({@ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "long", paramType = "query")})
    //@PostMapping(path = "/remove/{roleId}")
    //public WebResponse<Boolean> remove(HttpServletRequest request, @PathVariable long roleId) throws BusinessException {
    //    return new WebResponse<>(operateRoleService.remove(roleId));
    //}

    @WebAccess(login = true)
    @ApiOperation(value = "查询权限菜单tree", httpMethod = "GET")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @GetMapping(path = "/tree/permission")
    public WebResponse<List<OperateTreePermissionVO>> listAllPermission(HttpServletRequest request) throws BusinessException {
        return new WebResponse<>(operatePermissionService.listAllPermission());
    }
}
